import json
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from domain.default_news import news_data
import pandas as pd
import matplotlib.pyplot as plt
from text_cluster_by_hanlp import get_news_list, start_hanlp_cluster
from text_cluster_by_kmeans import get_matrix, get_tf_idf, start_kmeans_cluster


def get_news_data():
    engine = create_engine('sqlite:///database/news.db')
    Session = sessionmaker(bind=engine)
    session = Session()
    news_list = session.query(news_data).all()
    session.close()
    return news_list


def get_result_json_string(input_path):
    result = [line.strip() for line in open(input_path, encoding='UTF-8').readlines()]
    return result[0]


def get_counter():
    return {
        "交通": 0, "体育": 0, "农业": 0, "医疗": 0, "历史": 0, "哲学": 0, "教育": 0, "文学": 0, "时政": 0,
        "法律": 0, "环境": 0, "电子": 0, "矿藏": 0, "空间": 0, "经济": 0, "能源": 0, "艺术": 0, "计算机": 0,
        "通信": 0
    }


def get_evaluate_for_hanlp(input_path):
    news_list = get_news_data()
    result_string = get_result_json_string(input_path)
    result_json = json.loads(result_string)
    evaluate_dict_list = []
    for item_list in result_json:
        evaluate_dict = get_counter()
        for item in item_list:
            new_item = news_list[int(item) - 1]
            evaluate_dict[new_item.type] += 1
        evaluate_dict_list.append(evaluate_dict)
    return evaluate_dict_list


def export_to_excel(evaluate_dict_list, output_path):
    df = pd.DataFrame(evaluate_dict_list)
    df.to_excel(output_path, index=False, encoding="utf-8")


def hanlp_evaluate():
    i = 1
    while i < 10:
        content_list = get_news_list()
        start_hanlp_cluster(content_list, './data/results' + str(i) + '.txt', 19)
        evaluate_dict_list = get_evaluate_for_hanlp('./data/results' + str(i) + '.txt')
        export_to_excel(evaluate_dict_list, "./data/evaluate_result" + str(i) + ".xlsx")
        i += 1


def evaluate_k_num():
    matrix = get_matrix()
    tf_idf_matrix = get_tf_idf(matrix)
    inertia_list = []
    x_list = []
    for i in range(2, 30):
        result = start_kmeans_cluster(tf_idf_matrix, i)
        inertia_list.append(result.inertia_)
        x_list.append(i)
    plt.plot(x_list, inertia_list)
    plt.show()


def get_evaluate_for_kmeans():
    news_list = get_news_data()
    matrix = get_matrix()
    tf_idf_matrix = get_tf_idf(matrix)
    result = start_kmeans_cluster(tf_idf_matrix, 25)
    labels = result.labels_.tolist()
    evaluate_list = [[] for x in range(25)]
    for i in range(len(labels)):
        evaluate_list[labels[i]].append(i)

    evaluate_dict_list = []
    for item_list in evaluate_list:
        evaluate_dict = get_counter()
        for item in item_list:
            new_item = news_list[int(item)]
            evaluate_dict[new_item.type] += 1
        evaluate_dict_list.append(evaluate_dict)
    return evaluate_dict_list


def kmeans_evaluate():
    evaluate_dict_list = get_evaluate_for_kmeans()
    export_to_excel(evaluate_dict_list, "./data/kmeans_evaluate_result" + ".xlsx")


if __name__ == '__main__':
    kmeans_evaluate()